//
//  DriverListCell.m
//  LogisticsSupplier
//
//  Created by 吕晶 on 2020/8/5.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "DriverListCell.h"

@interface DriverListCell ()

@property (nonatomic, strong) UIImageView *iconImgView;
@property (nonatomic, strong) UILabel *nameLab;
@property (nonatomic, strong) UIImageView *phoneImgView;
@property (nonatomic, strong) UILabel *phoneLab;
@property (nonatomic, strong) UIButton *phoneBtn;
@property (nonatomic, strong) UIButton *operationBtn;
@property (nonatomic, strong) UILabel *line;

@end

@implementation DriverListCell

+ (instancetype)DriverListCellWithTableView:(UITableView *)tableView {
    static NSString *cellID = @"DriverListCell";
    DriverListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[DriverListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = HexColor(0xffffff);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.iconImgView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 20, 50, 50)];
        self.iconImgView.backgroundColor = Color_backgroundColor;
        self.iconImgView.layer.masksToBounds = YES;
        self.iconImgView.layer.cornerRadius = 25;
        self.iconImgView.contentMode = UIViewContentModeScaleAspectFill;
        [self.contentView addSubview:self.iconImgView];
        
        self.nameLab = [[UILabel alloc] initWithFrame:CGRectMake(self.iconImgView.nim_right + 10, self.iconImgView.nim_top + 6.5, ScreenW - 15 - 64 - 20 - 30 - 10 - (self.iconImgView.nim_right + 10), 17)];
        self.nameLab.font = BoldFontOfSize(16);
        self.nameLab.textColor = HexColor(0x2B2C44);
        [self.contentView addSubview:self.nameLab];
        
        self.phoneImgView = [[UIImageView alloc] initWithFrame:CGRectMake(self.nameLab.nim_left, self.nameLab.nim_bottom + 5 + 1.5, 12, 12)];
        self.phoneImgView.image = [UIImage imageNamed:@"icon_driver_phone"];
        [self.contentView addSubview:self.phoneImgView];
        
        self.phoneLab = [[UILabel alloc] initWithFrame:CGRectMake(self.phoneImgView.nim_right + 6, self.nameLab.nim_bottom + 5, self.nameLab.nim_right - (self.phoneImgView.nim_right + 6), 15)];
        self.phoneLab.textColor = HexColor(0x9C9DB3);
        self.phoneLab.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.phoneLab];
        
        self.phoneBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.nameLab.nim_right + 10, 30, 30, 30)];
        [self.phoneBtn addTarget:self action:@selector(phoneClick) forControlEvents:UIControlEventTouchUpInside];
        [self.phoneBtn setImage:[UIImage imageNamed:@"icon_driver_phone_2"] forState:0];
        [self.contentView addSubview:self.phoneBtn];
        
        self.operationBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.phoneBtn.nim_right + 20, 30, 64, 30)];
        self.operationBtn.titleLabel.font = BoldFontOfSize(14);
        self.operationBtn.layer.cornerRadius = 15;
        [self.operationBtn addTarget:self action:@selector(operationClick) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.operationBtn];
        
        self.line = [[UILabel alloc] initWithFrame:CGRectMake(15, 89, ScreenW - 30, 1)];
        self.line.backgroundColor = Color_backgroundColor;
        [self.contentView addSubview:self.line];
        
        self.height = self.line.nim_bottom;
    }
    return self;
}

- (void)phoneClick {
    if (self.model.phone.length) {
        NSString *str = [[NSMutableString alloc] initWithFormat:@"tel:%@",self.model.phone];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str] options:@{} completionHandler:nil];
    }
}

- (void)operationClick {
    if (self.operationBlock) {
        self.operationBlock();
    }
}

-(void)setModel:(DriverMainModel *)model {
    _model = model;
    self.nameLab.text = model.name;
    self.phoneLab.text = model.phone;
    [self.iconImgView sd_setImageWithURL:[NSURL URLWithString:model.headimg]];
    if (model.status == DriverStatusCanUse) {
        //已启用
        [self.operationBtn setTitle:@"禁用" forState:0];
        [self.operationBtn setTitleColor:HexColor(0xF35959) forState:0];
        [self.operationBtn setBackgroundColor:HexColorA(0xF35959, 0.1)];
    }else if (model.status == DriverStatusNoUse) {
        //已禁用
        [self.operationBtn setTitle:@"启用" forState:0];
        [self.operationBtn setTitleColor:HexColor(0xffffff) forState:0];
        [self.operationBtn setBackgroundColor:Color_ThemeColor];
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
